package schoolMatSupport;

import java.io.File;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.concurrent.ExecutionException;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.document.DocumentFamily;
import org.artofsolving.jodconverter.document.DocumentFormat;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;



/**
 *
 * @author Michal Horak
 */
public class ConvertFileToPDF {

    private OfficeManager officeManager;

    public File fileToPDF(final String checkSum, final String serverPath, final File file) throws InterruptedException, ExecutionException {

        final File inputFile = new File(file.getPath());
        
        File fileDir = new File(serverPath + "resources/materials" + File.separator + checkSum);
        
        //pokud slozka neexistuje, vytvorime je
        if (!fileDir.exists()){
            fileDir.mkdir();
        }
        
        final File outputFile = new File(fileDir.getPath() + File.separator + checkSum+ "_tmp.pdf");

        PropertyLoader pl = PropertyLoader.getInstance(serverPath);
        String openUrl = pl.getProperty("openOffice");
        
        System.out.println(openUrl);

        //pokusim se nalezt spousteci soubor k soffice kvuli spusteni sluzby
        File openFile = new File(openUrl);
        try {
            officeManager = new DefaultOfficeManagerConfiguration().setOfficeHome(openFile).setTaskExecutionTimeout(120000L).buildOfficeManager();
            officeManager.start();
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e.getMessage());
        } catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }

        // convert
        final OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        final String writer = "writer_pdf_Export";
        final String impress = "impress_pdf_Export";


        DocumentFormat format = null;
        if (file.getName().endsWith(".docx")
                || file.getName().endsWith(".doc")
                || file.getName().endsWith(".odt")
                || file.getName().endsWith(".txt")
                || file.getName().endsWith(".rtf")
                || file.getName().endsWith(".fodt")) {
            format = toFormatPDFA(DocumentFamily.TEXT, writer);
        } else if (file.getName().endsWith(".odp")
                || file.getName().endsWith(".fodp")
                || file.getName().endsWith(".ppt")
                || file.getName().endsWith(".pptx")) {

            format = toFormatPDFA(DocumentFamily.PRESENTATION, impress);
        }

        converter.convert(inputFile, outputFile, format);

        officeManager.stop();
        
        return outputFile;
    }

    public OfficeManager getConnection() {
        return this.officeManager;
    }

    private DocumentFormat toFormatPDFA(DocumentFamily exportFilter, String filter) {
        // create a PDF DocumentFormat (as normally configured in document-formats.xml)
        DocumentFormat customPdfFormat = new DocumentFormat("Portable Document Format", "application/pdf", "pdf");

        // now set our custom options
        LinkedHashMap<String, Object> aFilterMap = new LinkedHashMap<String, Object>();
        aFilterMap.put("FilterName", filter);
        aFilterMap.put("FilterData",
                Collections.singletonMap("SelectPdfVersion", Integer.valueOf(1)));
        customPdfFormat.setStoreProperties(exportFilter, aFilterMap);

        return customPdfFormat;
    }

    public boolean isSupported(File file) {
        if (file.getName().endsWith(".doc")
                || file.getName().endsWith(".docx")
                || file.getName().endsWith(".odp")
                || file.getName().endsWith(".txt")
                || file.getName().endsWith(".rtf")
                || file.getName().endsWith(".fodp")
                || file.getName().endsWith(".fodt")
                || file.getName().endsWith(".odt")
                || file.getName().endsWith(".ppt")
                || file.getName().endsWith(".pptx")) {
            return true;
        }
        return false;
    }
}
